package com.myeclipse.hibernatespring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BusinessLogic {
	public static void main(String[] args) {
		/* 1. Create a new user */
		Integer id = new Integer(1);

		User user = new User();
		user.setId(id);
		user.setUsername("nitin");
		user.setPassword("gautam");
		user.setFirstName("Nitin");
		user.setLastName("Gautam");
		String s = Long.toString(System.currentTimeMillis());
		user.setDateCreated(Long.valueOf(s));
		/* 2. Load the Spring bean configuration and create a bean factory */
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"applicationContext.xml"));

		/* 3. Create instance of PersistenceLayer */
		PersistenceLayer persistenceLayer = (PersistenceLayer) beanFactory
				.getBean("persistenceLayer");

		/* 4. Save the new user to the database */
		persistenceLayer.addUser(user);

		/* 5. Confirm that our user was saved */
		User userLoadedFromDB = persistenceLayer.findUserById(id);
		System.out.println("User Loaded from DB [username="
				+ userLoadedFromDB.getUsername() + ", password="
				+ userLoadedFromDB.getPassword() + ", firstName="
				+ userLoadedFromDB.getFirstName() + ", lastName="
				+ userLoadedFromDB.getLastName() + ", Date="
				+ userLoadedFromDB.getDateCreated() + "]");

		/* 6. Update the user */
		userLoadedFromDB.setFirstName("Vikas");
		persistenceLayer.updateUser(userLoadedFromDB);

		/* 7. Confirm that the update worked */
		User userLoadedFromDBAgain = persistenceLayer
				.findUserById(id);
		System.out.println("User Loaded from DB Again [firstName="
				+ userLoadedFromDBAgain.getFirstName() + "]");

		/* 8. Delete the user */
		//persistenceLayer.deleteUser(user);
	}
}